<template>
    <div class="sensitive-words-container">
        <el-dialog title="修改敏感词词语" v-model="isShowDialog" >
            <el-form ref="editSensitiveWordsFormRef" :model="formData" :rules="formRules" label-width="100px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="敏感词词语" prop="words">
                            <el-input v-model="formData.words" placeholder="请输入敏感词词语" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { editSensitiveWords } from '/@/api/system/words/index';
export default {
    name: 'system/words/edit',
    setup(props, context) {
        const editSensitiveWordsFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            formData: {},
            formRules : {
            }
        });

        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            if(row){
                state.formData = Object.assign({}, row);
            }
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            editSensitiveWordsFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            editSensitiveWordsFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        id: state.formData.id,
                        words: state.formData.words,
                    }
                    editSensitiveWords(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            editSensitiveWordsFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>